package com.ruoyi.school.chapter.controller;

import java.util.List;

import com.ruoyi.school.course.service.ICourseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.school.chapter.domain.Chapter;
import com.ruoyi.school.chapter.service.IChapterService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.Ztree;

/**
 * 章节Controller
 * 
 * @author qiang
 * @date 2022-06-26
 */
@Controller
@RequestMapping("/school/chapter")
public class ChapterController extends BaseController
{
    private String prefix = "school/chapter";

    @Autowired
    private IChapterService chapterService;

    @Autowired
    private ICourseService courseService;

    @RequiresPermissions("school:chapter:view")
    @GetMapping()
    public String chapter()
    {
        return prefix + "/chapter";
    }

    /**
     * 查询章节树列表
     */
    @RequiresPermissions("school:chapter:list")
    @PostMapping("/list")
    @ResponseBody
    public List<Chapter> list(Chapter chapter)
    {
        List<Chapter> list = chapterService.selectChapterList(chapter);
        return list;
    }

    /**
     * 导出章节列表
     */
    @RequiresPermissions("school:chapter:export")
    @Log(title = "章节", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Chapter chapter)
    {
        List<Chapter> list = chapterService.selectChapterList(chapter);
        ExcelUtil<Chapter> util = new ExcelUtil<Chapter>(Chapter.class);
        return util.exportExcel(list, "章节数据");
    }

    /**
     * 新增章节
     */
    @GetMapping(value = { "/add/{id}", "/add/" })
    public String add(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("chapter", chapterService.selectChapterById(id));
        }
        mmap.put("courses",courseService.selectCourseList(null));

        return prefix + "/add2";
    }

    /**
     * 新增保存章节
     */
    @RequiresPermissions("school:chapter:add")
    @Log(title = "章节", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Chapter chapter)
    {
        return toAjax(chapterService.insertChapter(chapter));
    }

    /**
     * 修改章节
     */
    @RequiresPermissions("school:chapter:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Chapter chapter = chapterService.selectChapterById(id);
        mmap.put("chapter", chapter);
        return prefix + "/edit";
    }

    /**
     * 修改保存章节
     */
    @RequiresPermissions("school:chapter:edit")
    @Log(title = "章节", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Chapter chapter)
    {
        return toAjax(chapterService.updateChapter(chapter));
    }

    /**
     * 删除
     */
    @RequiresPermissions("school:chapter:remove")
    @Log(title = "章节", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        return toAjax(chapterService.deleteChapterById(id));
    }

    /**
     * 选择章节树
     */
    @GetMapping(value = { "/selectChapterTree/{id}", "/selectChapterTree/" })
    public String selectChapterTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("chapter", chapterService.selectChapterById(id));
        }
        return prefix + "/tree";
    }

    /**
     * 加载章节树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = chapterService.selectChapterTree();
        return ztrees;
    }

    @GetMapping("/treeData/{id}")
    @ResponseBody
    public List<Ztree> treeData(@PathVariable(value="id",required = true)Long courseId)
    {
        System.out.println("courseId="+courseId);
        List<Ztree> ztrees = chapterService.selectChapterTree(courseId);
        return ztrees;
    }
}
